<?php
	include_once '../../../include/inc_session.php';

	if(isset($_GET['Identifiant_personne']) && !empty($_GET['Identifiant_personne']))
	{ // Vérifier si Identifiant_personne est passé en paramètre
		$idPersonne = $_GET['Identifiant_personne'];
	}
	$idRole = $_GET['idrole'];
	$idContrat = $_GET['idcontrat'];
	$idAdr = $_GET['Identifiant_adresse'];
	$oAdresse = new dataObject_adresse($idAdr);
	
	$aAdresses = $oAdresse->adresse_getActiveListForPersonne($idPersonne);

	$TYPE_ADR    =  (isset($_POST['Type_adresse']))  ?  $_POST['Type_adresse']  : $oAdresse->_TYPE_ADR;
	$LIGNE_UN    =  (isset($_POST['OA1_Ligne1_adresse'])) ?  $_POST['OA1_Ligne1_adresse'] : $oAdresse->_LIGNE1_ADR;
	$LIGNE_DEUX  =  (isset($_POST['OA2_Ligne2_adresse'])) ?  $_POST['OA2_Ligne2_adresse'] : $oAdresse->_LIGNE2_ADR;
	$LIGNE_TROIS = 	(isset($_POST['OA3_Ligne3_adresse'])) ?  $_POST['OA3_Ligne3_adresse'] : $oAdresse->_LIGNE3_ADR;
	
	
	if($gbFormValid)
	{
		
		$oAdresse->_TYPE_ADR 				= $TYPE_ADR;
		$oAdresse->_ETAT_ADR 				= $_POST['Etat_adresse'];
		$oAdresse->_LIGNE1_ADR 				= $LIGNE_UN;
		$oAdresse->_LIGNE2_ADR 				= $LIGNE_DEUX;
		$oAdresse->_LIGNE3_ADR				= $LIGNE_TROIS;
		$oAdresse->_IDENTIFIANT_PERSONNE	= $_POST['Identifiant_personne'];
		
		$id = $oAdresse->adresse_update();
		
		if($id ===false) $oForm->ajouterErreur(chgmt_couv_crea_adresse_pbm);
		else $oForm->ajouterErreur(adresse_modif);
	}

	// Suppression de l'adresse (rendre l'état de l'adresse Inactif : I)
	if(isset($_GET["supp"]) && $_GET["supp"]=="1" && isset($_GET["Identifiant_adresse"]) && isset($_GET["Identifiant_personne"]))
	{
		$nombreAdrActive = count($aAdresses);
		if($nombreAdrActive<=1)
		{ // Vérifier si le partenaire a au moins 2 adresse actives (état à A)
			$deleteGoTo = "index.php?Identifiant_personne=".$_GET['Identifiant_personne']."&idcontrat=".$idContrat."&idrole=".$idRole."&msg=no";
		}
		else
		{
			$iResult = $oAdresse->adresse_desactive();
			if($iResult)
			{
				$deleteGoTo = "./index.php?Identifiant_adresse=". $_GET['Identifiant_adresse'] ."&Identifiant_personne=". $_GET['Identifiant_personne']."&idcontrat=".$idContrat."&idrole=".$idRole."&msg=supp";
			}
			else
			{
				$deleteGoTo = "./index.php?Identifiant_adresse=". $_GET['Identifiant_adresse'] ."&Identifiant_personne=". $_GET['Identifiant_personne']."&idcontrat=".$idContrat."&idrole=".$idRole."&msg=pbm";
			}
			
		} // Fin else $nombreAdrActive<=2
		header(sprintf("Location: %s", $deleteGoTo));
	}

	$nom_module_saisie="Détail adresse";
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $oFonction->_MENU_AFFICHE; ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="list.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
	</head>
<body>
<div id="sf_menu1">
	<h3><?php echo $oBloc->_MENU_NOM ?></h3>
	<div id="information_erreur" class="false">
        <ul id="message">
        	<li><?php $aMsg =$oForm->getValidMessage(); echo (!empty($aMsg))? $aMsg[0]['message'] : ''; ?></li>
        </ul>
    </div>

</div>
<div class="cleared"></div>

<div class="corps1">
<div id="br"></div>
<!--/***************************************/-->

<form action="<?php echo $sEditFormAction; ?>" method="post" name="form1" id="form1">
	<div class="imgleft"></div>
	<div class="imgmiddle">
    <div class="btn">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $oFonction->_MENU_AFFICHE; ?></h5>
        </span><span class="btnright"></span>
    </div>
  	<div class="btn1">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nom_module_saisie; ?></h5>
        </span>
        <span class="btnright"></span>
    </div>
	<div class="formulaire">
  <table class="tabform">
    <tr>
      <td>TYPE ADRESSE</td>
      <td><select name="Type_adresse">
<?php 
	$aTypeAdr = dataObject_elementdico::elementdico_getListFromCode(dataObject_dictionnaire::TYPE_ADRESSE);
	foreach ($aTypeAdr as $i => $aRow)
	{ 
		?>
				<option value="<?php echo $aRow['Code_element']; ?>" <?php if(isset($_POST['Type_adresse'])){if (!(strcmp($aRow['Code_element'], htmlentities($_POST['Type_adresse'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";}}else{ if (!(strcmp($aRow['Code_element'], htmlentities($oAdresse->_TYPE_ADR, ENT_COMPAT, 'utf-8')))) {echo "SELECTED";}} ?> ><?php echo $aRow['Nom_element']; ?></option>
<?php 
	} 
?>
      </select>
      </td>
    </tr>
    <input type="hidden" name="Etat_adresse" value="<?php echo htmlentities($oAdresse->_ETAT_ADR, ENT_COMPAT, 'utf-8') ?>" />
    <tr>
      <td>ADRESSE</td>
      <td><input type="text" name="OA1_Ligne1_adresse" value="<?php if(isset($_POST['OA1_Ligne1_adresse'])){ echo remind_text('OA1_Ligne1_adresse'); }else{ echo htmlentities(stripslashes($LIGNE_UN), ENT_COMPAT, 'utf-8');} ?>" size="32" maxlength="30" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="text" name="OA2_Ligne2_adresse" value="<?php if(isset($_POST['OA2_Ligne2_adresse'])){ echo remind_text('OA2_Ligne2_adresse'); }else{ echo htmlentities(stripslashes($LIGNE_DEUX), ENT_COMPAT, 'utf-8');} ?>" size="32" maxlength="30" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="text" name="OA3_Ligne3_adresse" value="<?php if(isset($_POST['OA3_Ligne3_adresse'])){ echo remind_text('OA3_Ligne3_adresse'); }else{ echo htmlentities(stripslashes($LIGNE_TROIS), ENT_COMPAT, 'utf-8');} ?>" size="32" maxlength="30" /></td>
    </tr>
	<input type="hidden" name="Identifiant_personne" value="<?php echo htmlentities($oAdresse->_IDENTIFIANT_PERSONNE, ENT_COMPAT, 'utf-8'); ?>" />
  </table>
    </div>
</div>
<div class="imgright"></div>				
<div class="cleared"></div>

<div class="bt1">
	<a href="../adresse/index.php?Identifiant_personne=<?php echo $_GET['Identifiant_personne']; ?>&idcontrat=<?php echo $idContrat; ?>&idrole=<?php echo $idRole; ?>">
    	<span class="btleft"></span>
        <span class="btmiddle">Annuler</span>
        <span class="btright"></span>
    </a>
</div>

<div class="bt">
	<span class="btleft"></span>
    <input type="submit" name="Valider" value="Valider" class="resizeall btmiddle"/>
    <span class="btright"></span>
</div>
    
<div class="cleared"></div>

  <input type="hidden" name="Identifiant_adresse" value="<?php echo $idAdr; ?>" />
  <input type="hidden" name="MM_update" value="form1" />

</form>
 </div>
</body>
</html>
<?php

include_once PATH_INCLUDE.'inc_pied_page.php';
?>
